package com.example.ffepeg.ffpeg;

import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoInfo;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;

public class GetVideoTime {
    private static void input(){
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/main/resources/file/txt.txt"))) {
            int byteContent;
            while ((byteContent = bis.read()) != -1) {
                System.out.print((char) byteContent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void output(){
        String content = "Hello, world!";
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/main/resources/file/txt.txt"))) {
            bos.write(content.getBytes());
            // 数据被写入到缓冲区，并不一定立即写入到文件
            bos.flush(); // 强制将缓冲区的数据写入到文件
            System.out.println("数据写入成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        output();
        //test01();
        //System.out.println(String.valueOf(System.currentTimeMillis()));
    }
    public static void test01() {
        // 视频时长
        long time = 0;
        float frameRate = 0;
        try {
            String url = "https://pushimg.liecaizhijia.com/adminshop/20250325/b1523efb6c5b9f650328cce4d790038a.mp4";
            MultimediaObject media = new MultimediaObject(new URL(url));
            MultimediaInfo info = media.getInfo();
            // 时长，毫秒级
            long duration = info.getDuration();
            // 毫秒级时长转化为秒
            BigDecimal bigDecimal1 = new BigDecimal(duration);
            BigDecimal bigDecimal2 = new BigDecimal(1000);
            // 四舍五入，只保留整数
            time = bigDecimal1.divide(bigDecimal2, 0, RoundingMode.HALF_UP).longValue();
            // 获取媒体视频对象
            VideoInfo video = info.getVideo();
            // 码率
            int bitRate = video.getBitRate();
            // 帧率
            frameRate = video.getFrameRate();
            // 分辨率-高
            int height = video.getSize().getHeight();
            // 分辨率-宽
            int width = video.getSize().getWidth();
            // 视频解码器名称
            String decoder = video.getDecoder();
        } catch (Exception e) {
            e.getMessage();
        }
        System.out.println("时长：" + time);
        System.out.println("帧率：" + frameRate);
    }

}
